﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class AddSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    IfRemarksIsYesOrNo();
                    List<int> resultSetForStaffID = StaffHousekeepingDBManager.getStaffIDHousekeepingList();
                    staffIDListBox.DataSource = resultSetForStaffID;
                    staffIDListBox.DataBind();
                    List<int> resultSetForRoomNo = StaffHousekeepingDBManager.getRoomNumList();
                    roomNumListBox.DataSource = resultSetForRoomNo;
                    roomNumListBox.DataBind();
                    roomNumListBox.Enabled = false;
                    RequiredFieldValidator2.Enabled = false;
                }
            }
        }

        protected void staffIDListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            stfNameLabel.Text = StaffHousekeepingDBManager.getStaffNameByStaffIDHousekeeping(Convert.ToInt32(staffIDListBox.SelectedValue));
            string resultPosition = StaffHousekeepingDBManager.getStaffPositionHousekeeping(Convert.ToInt32(staffIDListBox.SelectedValue));
            if (resultPosition != null)
            {
                stfPositionLabel.Text = resultPosition;
            }
            else
            {
                stfPositionLabel.Text = "Staff has no position!";
            }
            bool ifStaffRoom = StaffHousekeepingDBManager.ifStaffIsRoom(Convert.ToInt32(staffIDListBox.SelectedValue));
            if (ifStaffRoom == true)
            {
                roomNumListBox.Enabled = true;
                RequiredFieldValidator2.Enabled = true;
            }
            else
            {
                roomNumListBox.Enabled = false;
                RequiredFieldValidator2.Enabled = false;
            }
        }

        protected void submitSchedBtn_Click(object sender, EventArgs e)
        {
            Housekeeping sched = new Housekeeping();
            sched.staffID = Convert.ToInt32(staffIDListBox.SelectedValue);
            if (stfPositionLabel.Text == "Room Maintenance")
            {
                sched.roomNo = Convert.ToInt32(roomNumListBox.SelectedValue);
            }
            sched.remarks = remarksTextBox.Text;
            sched.dutyDateTime = DateTime.Parse(jobDateTextbox.Text + " " + jobTimeHoursDropDown.SelectedValue + ":" + jobTimeMinsTextbox.Text);
            int outcome = StaffHousekeepingDBManager.addSchedule(sched, stfPositionLabel.Text);
            if (outcome == 1)
            {
                outcomeLabel.Text = "Schedule successfully created! Redirecting to Manage Schedule page...";
                Timer1.Enabled = true;
            }
            else
            {
                outcomeLabel.Text = "Schedule not successfully created!";
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewSchedule.aspx");
        }

        protected void scheduleRadioButton_SelectedIndexChanged(object sender, EventArgs e)
        {
            IfRemarksIsYesOrNo();
        }

        protected void IfRemarksIsYesOrNo()
        {
            if (remarksRadioButton.SelectedValue == "Yes")
            {
                if (remarksTextBox.Enabled == false)
                {
                    remarksTextBox.Enabled = true;
                }
            }
            else
            {
                if (remarksTextBox.Enabled == true)
                {
                    remarksTextBox.Enabled = false;
                }
            }
        }

        protected void backToViewScheduleBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/ViewSchedule.aspx");
        }
    }
}